package com.gandong8.hbase.tool.config;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import java.io.IOException;

/**
 * 配置类：连接HBase的增删改查Connect类
 */
@org.springframework.context.annotation.Configuration
public class ConfDemo {

    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Value("${hbase.zookeeper.quorum}")
    private String zookeeperQuorum;

    @Value("${hbase.zookeeper.property.clientPort}")
    private String clientPort;

    @Bean("myConnection")
    public Connection getCon(){
        Configuration entries = HBaseConfiguration.create();
        entries.set("hbase.zookeeper.quorum",zookeeperQuorum);
        entries.set("hbase.zookeeper.property.clientPort",clientPort);
        try {
            Connection connection = ConnectionFactory.createConnection(entries);
            logger.info("创建hbase连接实例成功："+connection);
            return connection;
        } catch (IOException e) {
            logger.error("创建hbase连接实例失败");
            e.printStackTrace();
        }
        logger.error("创建hbase连接实例失败");
        return null;
    }

}
